package xmtTest;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import epam_java.jsontest.UserTest;
import java.util.ArrayList;
import java.util.Arrays;
import org.xml.sax.SAXException;

public class XMLParser {

    public static ArrayList<UserTest> parse(String filename) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(true);
        factory.setNamespaceAware(true);
            final String JAXP_SCHEMA_LANGUAGE =
               "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
            final String W3C_XML_SCHEMA =
               "http://www.w3.org/2001/XMLSchema";
            factory.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
        DocumentBuilder builder = factory.newDocumentBuilder();
        File f = new File(filename);
        Document doc = builder.parse(f);
        Element elem = doc.getDocumentElement();
        NodeList nodelist = elem.getChildNodes();
        ArrayList<UserTest> users = new ArrayList<UserTest>();
        for (int i = 0; i < nodelist.getLength(); i++) {
            Node child = nodelist.item(i);
            if (child instanceof Element) {
                UserTest user = new UserTest();
                //      System.out.println(""+child.getNodeName());
                NodeList list = child.getChildNodes();
                for (int k = 0; k < list.getLength(); k++) {
                    Node usernode = list.item(k);
                    if (usernode instanceof Element) {
                        Element el = (Element) usernode;
                        if (usernode.getNodeName().compareTo("userId") == 0) {
                            //                System.out.println(""+el.getTextContent());
                            user.setUserId(Long.parseLong(usernode.getTextContent()));
                        } else if (usernode.getNodeName().compareTo("userName") == 0) {
                            user.setUserName(usernode.getTextContent());
                        } else if (usernode.getNodeName().compareTo("login") == 0) {
                            user.setLogin(usernode.getTextContent());
                        } else if (usernode.getNodeName().compareTo("currentAcces") == 0) {
                            user.setCurrentAcces(usernode.getTextContent());
                        } else if (usernode.getNodeName().compareTo("doublefavouriteNumbers") == 0) {
                            NodeList doublenumbers = usernode.getChildNodes();
                            ArrayList<Double> d = new ArrayList<Double>();
                            for (int j = 0; j < doublenumbers.getLength(); j++) {
                                if (doublenumbers.item(j) instanceof Element) {
                                    double dtemp = Double.parseDouble(doublenumbers.item(j).getTextContent().trim());
                                    d.add(dtemp);
                                }
                            }
                            double darray[] = new double[d.size()];
                            for (int j = 0; j < darray.length; j++) {
                                darray[j] = d.get(j).doubleValue();
                            }
                            user.setDoublefavouriteNumbers(darray);
                            System.out.println("" + Arrays.toString(user.getDoublefavouriteNumbers()));
                        } else if (usernode.getNodeName().compareTo("intfavouriteNumbers") == 0) {
                            NodeList intnumbers = usernode.getChildNodes();
                            ArrayList<Integer> integ = new ArrayList<Integer>();
                            for (int j = 0; j < intnumbers.getLength(); j++) {
                                if (intnumbers.item(j) instanceof Element) {
                                    int inttemp = Integer.parseInt(intnumbers.item(j).getTextContent().trim());
                                    integ.add(inttemp);
                                }
                            }
                            int intarray[] = new int[integ.size()];
                            for (int j = 0; j < intarray.length; j++) {
                                intarray[j] = integ.get(j);
                            }
                            user.setIntfavouriteNumbers(intarray);
                        }
                    }
                }
                users.add(user);
            }
        }
        return users;
    }
}
